package com.wise.site.detail;

import java.util.ArrayList;
import java.util.List;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;
import org.taptwo.android.widget.ViewFlow.OnScrollListener;

import com.wise.site.R;
import com.wise.site.adapter.SaleImageAdapter;
import com.wise.site.adapter.SupplyImageAdapter;
import com.wise.site.main.MainIndexActivity;
import com.wise.site.protocol.action.BusinessAction;
import com.wise.site.protocol.action.BusinessDetailsAction;
import com.wise.site.protocol.action.ImageItemsAction;
import com.wise.site.protocol.action.ShopDetailAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.result.BusinessDetailsResult;
import com.wise.site.protocol.result.BussnissItem;
import com.wise.site.protocol.result.ImageItemsResult;
import com.wise.site.protocol.result.LoginResult;
import com.wise.site.protocol.result.ShopDetailResult;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.utils.CollectorUtils;
import com.wise.site.utils.CommentUtils;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.DataCache;
import com.wise.site.utils.CollectorUtils.UploadData;
import com.wise.site.utils.CommentUtils.CommentUploadData;
import com.wise.site.widget.HeadlineScrollView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SupplyDetailsActivity extends Activity implements View.OnClickListener {
	
//	private BusinessDetailsAction mAction;
	private ViewFlow mViewFlow;
	private BussnissItem mItem;
	private TextView commentText;
	private View shareBnt;
	private View collectBnt;
	private View submitBnt;
	private HeadlineScrollView headlineScroll;
	//private TextView mflowCount;
	//private int mCount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supply_details_activity);
		char c[] = getString(R.string.company).toCharArray();
		String company = c[0] + "　　" + c[1] + " :";
		((TextView)findViewById(R.id.company_lab)).setText(company);
		
		char price[] = getString(R.string.price).toCharArray();
		((TextView)findViewById(R.id.supply_details_price_lab)).setText(price[0] + "　　" + price[1] + "  :");
		
		char favorite[] = getString(R.string.favorite).toCharArray();
		((TextView)findViewById(R.id.supply_details_favorite_lab)).setText(favorite[0] + "　　" + favorite[1] + "  :");
		
		char introduction[] = getString(R.string.introduction).toCharArray();
		((TextView)findViewById(R.id.supply_details_introduction_lab)).setText(introduction[0] + "　　" + introduction[1] + "  :");
		
		
		findViewById(R.id.phone_layout).setOnClickListener(this);
		findViewById(R.id.title_back).setOnClickListener(this);
		findViewById(R.id.activity_title_comment).setOnClickListener(this);
		findViewById(R.id.company_panel).setOnClickListener(this);
		((TextView)findViewById(R.id.activity_title)).setText(getString(R.string.detail));
		headlineScroll =  (HeadlineScrollView) findViewById(R.id.headline_scroll);
		//mflowCount = (TextView) findViewById(R.id.flow_count);
		
		shareBnt = findViewById(R.id.share_img);
		shareBnt.setOnClickListener(this);
		collectBnt = findViewById(R.id.collect_img);
		collectBnt.setOnClickListener(this);
		submitBnt = findViewById(R.id.sub_comment_bnt);
		submitBnt.setOnClickListener(this);
		commentText = ((TextView)findViewById(R.id.info_comment));
		commentText.setOnClickListener(this);
		
		Intent intent = getIntent();
		mItem = (BussnissItem) intent.getSerializableExtra(Constanct.INFO_ENTRY);
		
		bindData(mItem);
		
//		mAction = new BusinessDetailsAction(ACTION_TYPE.ACTION_COMMON, "getSupplyDetails");
//		
//		mAction.setActionListener(new ActionListener<BusinessDetailsResult>() {
//
//			@Override
//			public void onSucceed(BusinessDetailsResult result) {
//				// TODO Auto-generated method stub
//				if(result == null){
//					return ;
//				}
//				bindData(result);
//			}
//
//			@Override
//			public void onError(int resultCode) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//		ProtocolManager.getProtocolManager().submitAction(mAction);
		
		
		ShopDetailAction action = new ShopDetailAction(ACTION_TYPE.ACTION_COMMON, "getShopDetail");
		action.setActionListener(new ActionListener<ShopDetailResult>() {

			@Override
			public void onSucceed(ShopDetailResult result) {
				// TODO Auto-generated method stub
				bindShopInfo(result);
			}

			@Override
			public void onError(int resultCode) {
				// TODO Auto-generated method stub
				
			}
		});
		ProtocolManager.getProtocolManager().submitAction(action);
	}
	
	
	private void bindData(BussnissItem result){
		((TextView)findViewById(R.id.supply_details_title)).setText(result.title);
		((TextView)findViewById(R.id.supply_details_price)).setText(result.price);
		((TextView)findViewById(R.id.supply_details_favorite)).setText(result.favoraterNum + "");
		((TextView)findViewById(R.id.supply_details_content)).setText(result.description);
		((TextView)findViewById(R.id.company_txt)).setText(result.company);
		((TextView)findViewById(R.id.phone_txt)).setText(result.phone);
		
		List<String> list = new ArrayList<String>();
		if(result.iconURL != null){
			for (String url : result.iconURL) {
				list.add(url);
			}
		}
		
		//测试数据开始
		/*list.add("" + R.drawable.index_normal_bg);
		list.add("" + R.drawable.headline_clicked);
		list.add("" + R.drawable.dota);
		list.add("" + R.drawable.loading_logo);
		list.add("" + R.drawable.login_btn_click);
		list.add("" + R.drawable.login_btn_sina);
		list.add("" + R.drawable.login_btn_tx);*/
		//测试数据结束
		
		//mCount = list.size();
		//mflowCount.setText("0/" + mCount);
		if(list.size() == 0){
			findViewById(R.id.viewflowindic).setVisibility(View.GONE);
			return;
		}
		
		OnScrollListener onScrollListener = new OnScrollListener() {
			@Override
			public void onStartScroll(View v) {
				headlineScroll.setScrollDisable(v, true);
			}
			
			@Override
			public void onScrollFinish(View v) {
				headlineScroll.setScrollDisable(null, false);
			}
		};
		mViewFlow = (ViewFlow) findViewById(R.id.view_flow);
		CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
		indic.setVisibility(View.VISIBLE);
		mViewFlow.setFlowIndicator(indic);
		mViewFlow.setOnScrollListener(mOnScrollListener);
		SupplyImageAdapter adapter = new SupplyImageAdapter(this);
		adapter.setList(list);
		mViewFlow.setAdapter(adapter);
		mViewFlow.setOnScrollListener(onScrollListener);
	}
	
	private void bindShopInfo(ShopDetailResult result){
		((TextView)findViewById(R.id.company_txt)).setText(result.getTitle());
		((TextView)findViewById(R.id.phone_txt)).setText(result.getPhoneNumber());
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
			shareBnt.setVisibility(View.VISIBLE);
			collectBnt.setVisibility(View.VISIBLE);
			submitBnt.setVisibility(View.GONE);
		}
		return super.dispatchKeyEvent(event);
	}
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		Intent intent = new Intent();
		switch(id){
		case R.id.info_comment:
			if(DataCache.getInstance().get(Constanct.ISLOGINED_KEY) == null){
				sendBroadcast(new Intent("sw.login"));
				finish();
				break;
			}
			shareBnt.setVisibility(View.GONE);
			collectBnt.setVisibility(View.GONE);
			submitBnt.setVisibility(View.VISIBLE);
			break;
		case R.id.company_panel:
			intent.putExtra(Constanct.INTENT_ID, mItem.shopId);
			intent.setClass(this, ShopsDetailsActivity.class);
			startActivity(intent);
			break;
		case R.id.sub_comment_bnt:
			LoginResult lr = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
			if(lr == null){
				sendBroadcast(new Intent("sw.login"));
				finish();
				break;
			}
			CommentUtils.getInstance().uploadComment(new CommentUploadData(mItem.id, lr.id, lr.userName, commentText.getText().toString()), 1, null, this);
			shareBnt.setVisibility(View.VISIBLE);
			collectBnt.setVisibility(View.VISIBLE);
			submitBnt.setVisibility(View.GONE);
			commentText.setText("");
			((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
			break;
		case R.id.phone_layout:
			intent.setAction(Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:" + ((TextView)findViewById(R.id.phone_txt)).getText().toString()));
			startActivity(intent);
			break;
		case R.id.title_back:
			finish();
			break;
		case R.id.share_img:
			intent.setClass(this, DetailShareActivity.class);
			startActivity(intent);
			break;
		case R.id.collect_img:
			if(mItem != null){
				LoginResult lr1 = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
				if(lr1 == null){
					startActivity(new Intent(getApplicationContext(), MainIndexActivity.class));
					sendBroadcast(new Intent("sw.login"));
					finish();
					break;
				}
				
				CollectorUtils.getInstance().uploadData(new UploadData(mItem.id, 1, lr1.id, mItem.userId), this);
			}
			break;
		case R.id.activity_title_comment:
			intent.setClass(this, DetailCommentActivity.class);
			intent.putExtra(Constanct.INFO_ENTRY, mItem.id);
			intent.putExtra(Constanct.INFO_TYPE, 1);
			intent.putExtra(Constanct.INTENT_TITLE, mItem.title);
			intent.putExtra(Constanct.INTENT_SUB_TITLE, mItem.description);
			startActivity(intent);
			break;
		}
	}
	
	
	private OnScrollListener mOnScrollListener = new OnScrollListener() {
		
		@Override
		public void onStartScroll(View fouceView) {
		}
		
		@Override
		public void onScrollFinish(View fouceView) {
			//mflowCount.setText("0" + "/" + mCount);
		}
	};

}
